//
//  YKClientDataBasicInfoView.m
//  YunKang
//
//  Created by leoking870 on 2017/11/30.
//  Copyright © 2017年 leoking870. All rights reserved.
//

#import "YKClientDataBasicInfoView.h"
#import "YKClientDataDetail.h"
#import "YKInputView.h"
#import "YKSelectView.h"
#import "YKOwnerShipFromModel.h"
#import "YKDatTypeModel.h"
#import "YKCustomLevelModel.h"
#import "YKMultiSelectDropdownView.h"
#import "YKRegionModel.h"

@interface YKClientDataBasicInfoView ()
@property(nonatomic, strong) YKClientDataDetail *dataDetail;
@end

@implementation YKClientDataBasicInfoView
- (instancetype)initWithClientModel:(YKClientDataDetail *)dataDetail {

    self = [super initWithFrame:CGRectZero];
    if (self) {
        _dataDetail = dataDetail;

        YKMedQuaModel *selectedMedQuaModel = [[[CommonDataManager sharedInstance].medQuaList tf_filteredWithPredicateBlock:^BOOL(YKMedQuaModel*  _Nonnull obj, NSInteger idx) {
            return [obj.id_p isEqualToString:self.dataDetail.MEDICAL_QUALIFICATION] || [obj.code isEqualToString:self.dataDetail.MEDICAL_QUALIFICATION];
        }]firstObject];


        YKSelectView* insuranceView = [self selectViewForList:[CommonDataManager sharedInstance].medQuaList  name:@"医保资质" compareBlock:^BOOL(id<YKDropdown> model) {
            return [[model id_p] isEqualToString:self.dataDetail.MEDICAL_QUALIFICATION] || [[model code] isEqualToString:self.dataDetail.MEDICAL_QUALIFICATION];
        }];
        insuranceView.tf_id = @"medical_qualification";
        self.dataDetail.MEDICAL_NAME = selectedMedQuaModel.MQ_NAME;

        [self addSubview:insuranceView];

        YKTypeModel *typeModels = [[[CommonDataManager sharedInstance].customerTypeList tf_filteredWithPredicateBlock:^BOOL(YKTypeModel *  _Nonnull obj, NSInteger idx) {
            return [self.dataDetail.CUSTOMERTYPE isEqualToString:obj.id_p] || [self.dataDetail.CUSTOMERTYPE isEqualToString:obj.code];
        }]firstObject];

        YKSelectView* categoryView = [[YKSelectView alloc] initWithTitle:@"合作模式" row:(NSArray <YKDropdown> *) [CommonDataManager sharedInstance].customerTypeList initialText:typeModels];
        categoryView.tf_id = @"customertype";
        [self addSubview:categoryView];


        YKSelectView* typeView = [self selectViewForList:[CommonDataManager sharedInstance].settlemenTypeList name:@"结算类型" compareBlock:^BOOL(id <YKDropdown> model) {
            return [self.dataDetail.SETTLEMENTTYPE isEqualToString:model.id_p] || [self.dataDetail.SETTLEMENTTYPE isEqualToString:model.code];
        }];
        [self addSubview:typeView];
        typeView.tf_id = @"settlementtype";
        YKSelectView* ownershipView = [self selectViewForList:[CommonDataManager sharedInstance].ownerShipFormList name:@"客户性质" compareBlock:^BOOL(id <YKDropdown> model) {
            return [self.dataDetail.OWNERSHIP_FORM isEqualToString:model.id_p];
        }];
        [self addSubview:ownershipView];
        ownershipView.tf_id = @"ownership_form";

        YKMultiSelectDropdownView *categoriesView = [[YKMultiSelectDropdownView alloc] initWithTitle:@"客户分类" modelData:(NSArray <YKDropdown> *) [CommonDataManager sharedInstance].customerCategoryList sameLine:YES];
        categoriesView.selectedCodes = [self.dataDetail.CATEGORY componentsSeparatedByString:@"|"];
        [self addSubview:categoriesView];
        categoriesView.tf_id = @"categoryArry";

        YKSelectView* dateTypeView = [self selectViewForList:[CommonDataManager sharedInstance].datTypeList name:@"是否连锁" compareBlock:^BOOL(id <YKDropdown> model) {
            return [self.dataDetail.DATTYPE isEqualToString:model.id_p];
        }];
        [self addSubview:dateTypeView];
        dateTypeView.tf_id = @"dattype";

        YKRegionModel *allRegion = [YKRegionModel new];
        allRegion.REGION_NAME = @"全部";
        allRegion.REGION_PATH = @"";
        allRegion.REGION_ID = @"";


        if ([CommonDataManager sharedInstance].regions) {

            NSMutableArray *data = [[@[allRegion] arrayByAddingObjectsFromArray:[CommonDataManager sharedInstance].regions] mutableCopy];
            for (YKRegionModel *model in data) {
                if (![[model.subMap.firstObject name] isEqualToString:@"全部"]) {
                    YKRegionModel *subAllRegion = [YKRegionModel new];
                    subAllRegion.REGION_NAME = @"全部";
                    subAllRegion.REGION_PATH = model.REGION_PATH;
                    subAllRegion.REGION_ID = @"";
                    subAllRegion.subMap = nil;
                    model.subMap = (NSArray <YKRegionModel> *) [@[subAllRegion] arrayByAddingObjectsFromArray:model.subMap];
                }
            }

            YKMultiSelectDropdownView *locationView = [[YKMultiSelectDropdownView alloc] initWithTitle:@"区域定位" modelData:(NSArray <YKDropdown> *) data sameLine:YES];
            locationView.selectedCodes = [self.dataDetail.REGION componentsSeparatedByString:@"|"];
            [self addSubview:locationView];
            locationView.tf_id = @"regionArry";
        }

        YKSelectView* customLevelView = [self selectViewForList:[CommonDataManager sharedInstance].customerLevelList name:@"客户等级" compareBlock:^BOOL(id <YKDropdown> model) {
            return [self.dataDetail.C_LEVEL isEqualToString:model.id_p];
        }];
        [self addSubview:customLevelView];
        customLevelView.tf_id = @"c_level";
        YKInputView* contactView = [[YKInputView alloc]initWithTitle:@"联系人" input:self.dataDetail.CONSIGNEE];
        [self addSubview:contactView];
        contactView.tf_id = @"consignee";
        YKInputView* phoneNumberView = [[YKInputView alloc]initWithTitle:@"手机号码" input:self.dataDetail.CONSIGNEE_PHONE];
        phoneNumberView.keyboardType = UIKeyboardTypePhonePad;
        phoneNumberView.maxInputCount = 11;
        [self addSubview:phoneNumberView];
        phoneNumberView.tf_id = @"consignee_phone";
        YKInputView* remarkTF = [[YKInputView alloc]initWithTitle:@"备注" input:self.dataDetail.REMARK];
        [self addSubview:remarkTF];
        remarkTF.tf_id = @"remark";
        for (UIView *view in self.subviews) {
            if ([view isKindOfClass:[YKInputView class]]) {
                ((YKInputView *) view).disableInputBackgroundColor = [UIColor tf_colorWithHexString:@"#f2f2f2"];
                ((YKInputView *) view).contentTopInset = 0;
                ((YKInputView *) view).editable = NO;
            }
            if ([view isKindOfClass:[YKSelectView class]]) {
                ((YKSelectView*)view).disableBackgroundColor = [UIColor tf_colorWithHexString:@"#f2f2f2"];
                ((YKSelectView*)view).contentTopInset = 0;
                ((YKSelectView*)view).enabled = NO;
            }
            if ([view isKindOfClass:[YKMultiSelectDropdownView class]]) {
                ((YKMultiSelectDropdownView*)view).disableBackgroundColor = [UIColor tf_colorWithHexString:@"#f2f2f2"];
                ((YKMultiSelectDropdownView*)view).contentTopInset = 0;
                ((YKMultiSelectDropdownView*)view).enabled = NO;
            }
        }



        [self.subviews mas_makeConstraints:^(MASConstraintMaker *make) {
            make.leading.trailing.equalTo(self);
            make.width.mas_equalTo(self.mas_width);
        }];
        [self.subviews mas_alignAlongAxis:MASAxisTypeVertical fixedSpace:@4 leadSpace:8 tailSpace:8];
    }
    return self;

}

- (NSDictionary *)values {
    NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
    for (UIView *view in self.subviews) {
        if ([view isKindOfClass:[YKInputView class]]) {
            dictionary[view.tf_id] = ((YKInputView *)view).input;
        }
        if ([view isKindOfClass:[YKSelectView class]]) {
            dictionary[view.tf_id] = ((YKSelectView *)view).selectedRow.id_p?:@"";
        }
        if ([view isKindOfClass:[YKMultiSelectDropdownView class]]) {
            if ([view.tf_id isEqualToString:@"categoryArry"]) {
                dictionary[@"categoryArry"] = [[((YKMultiSelectDropdownView *) view).selectedModels tf_mapUsingBlock:^id(id <YKDropdown> obj, NSInteger idx) {
                    return obj.code;
                }] componentsJoinedByString:@"|"];
                dictionary[@"categoryNameArry"] = [[((YKMultiSelectDropdownView *) view).selectedModels tf_mapUsingBlock:^id(id <YKDropdown> obj, NSInteger idx) {
                    return obj.name;
                }] componentsJoinedByString:@"-"];
            } else if ([view.tf_id isEqualToString:@"regionArry"]) {
                dictionary[@"regionArry"] = [[((YKMultiSelectDropdownView *) view).selectedModels tf_mapUsingBlock:^id(id <YKDropdown> obj, NSInteger idx) {
                    return obj.code;
                }] componentsJoinedByString:@"|"];
                dictionary[@"regionNameArry"] = [[((YKMultiSelectDropdownView *) view).selectedModels tf_mapUsingBlock:^id(id <YKDropdown> obj, NSInteger idx) {
                    return obj.name;
                }] componentsJoinedByString:@"-"];
            }
        }
    }
    return [dictionary copy];
}

- (void)setEditable:(BOOL)editable {
    _editable = editable;
    for (UIView *view in self.subviews) {
        if ([view isKindOfClass:[YKInputView class]]) {
            ((YKInputView *) view).editable = editable;
        }
        if ([view isKindOfClass:[YKSelectView class]]) {
            ((YKSelectView *) view).enabled = editable;
        }
        if ([view isKindOfClass:[YKMultiSelectDropdownView class]]) {
            ((YKMultiSelectDropdownView *) view).enabled = editable;
        }
    }
}

- (YKSelectView *)selectViewForList:(NSArray *)dataSource name:(NSString *)name compareBlock:(BOOL(^)(id<YKDropdown> model))compareBlock{
    id<YKDropdown> dropdown = [[dataSource tf_filteredWithPredicateBlock:^BOOL(YKCustomLevelModel * obj, NSInteger idx) {
        return compareBlock(obj);
    }] firstObject];
    YKSelectView* view = [[YKSelectView alloc] initWithTitle:name row:(NSArray <YKDropdown> *) dataSource initialText:dropdown];
    return view;
}


@end
